home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / envtools / cylenv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.6 KB  |  121 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    octenv -
  19.  *        Project an environment onto a cylinder
  20.  *
  21.  *                Paul Haeberli - 1988
  22.  */
  23. #include "texture.h"
  24.  
  25. #define DIV    1
  26. #define PI    (3.1415926535)
  27.  
  28. TEXTURE *tm;
  29. int size, samples;
  30.  
  31. main(argc,argv)
  32. int argc;
  33. char **argv;
  34. {
  35.     float vr, vg, vb;
  36.     float tr, tg, tb;
  37.     int x, y;
  38.     IMAGE *image;
  39.     int i;
  40.     vect pos, c;
  41.     int xsize, ysize;
  42.     short *rbuf, *gbuf, *bbuf;
  43.  
  44.     if(argc<5) {
  45.     fprintf(stderr,"usage: clyenv size samples file.env file.rgb\n"); 
  46.     exit(1);
  47.     }
  48.     size = atoi(argv[1]);
  49.     samples = atoi(argv[2]);
  50.     tm = tmopen(argv[3]);
  51.     if(!tm) {
  52.     printf("can't open environment map.\n");
  53.     exit(1);
  54.     }
  55.     xsize = size;
  56.     ysize = size;
  57.     rbuf = (short *)malloc(xsize*sizeof(short));
  58.     gbuf = (short *)malloc(xsize*sizeof(short));
  59.     bbuf = (short *)malloc(xsize*sizeof(short));
  60.     image = iopen(argv[4],"w",RLE(1),3,xsize,ysize,3);
  61.     for(y=0; y<ysize; y++) {
  62.     for(x=0; x<size; x++) {
  63.         tr = tg = tb = 0.0;
  64.         for(i=0; i<samples; i++) {
  65.         if(samples>1) {
  66.             pos.x = (x+frand())/xsize;
  67.             pos.y = (y+frand())/ysize;
  68.             pos.z = 0;
  69.         } else {
  70.             pos.x = (float)x/xsize;
  71.             pos.y = (float)y/ysize;
  72.             pos.z = 0.0;
  73.         }
  74.         shadecly(&pos,&c);
  75.         tr += c.x;
  76.         tg += c.y;
  77.         tb += c.z;
  78.         }
  79.         rbuf[x] = 255*tr/samples;
  80.         gbuf[x] = 255*tg/samples;
  81.         bbuf[x] = 255*tb/samples;
  82.     }
  83.     putrow(image,rbuf,y,0);
  84.     putrow(image,gbuf,y,1);
  85.     putrow(image,bbuf,y,2);
  86.     tpercentdone(100.0*y/(ysize-1));
  87.     }
  88.     iclose(image);
  89. }
  90.  
  91. float sinewarp(v)
  92. float v;
  93. {
  94.     return sin(v*(PI/2.0));
  95. }
  96.  
  97. shadecly(p,c)
  98. vect *p, *c; 
  99. {
  100.     float angle;
  101.     vect v;
  102.     float dx, dy, mag;;
  103.  
  104. #ifdef SDFSDF
  105.     p->x /= 2.0;
  106.     p->y = ((p->y-0.5)/2)+0.5;
  107. #endif
  108.  
  109.     dx = 1.0/(2*PI);
  110.     dy = p->y-0.5;
  111.     mag = sqrt(dx*dx+dy*dy);
  112.     dx /= mag;
  113.     dy /= mag;
  114.  
  115.     angle = 2*PI*p->x;
  116.     v.x = dx*sin(angle);
  117.     v.y = dx*cos(angle);
  118.     v.z = dy;
  119.     envsample(tm,&v,c);
  120. }
  121.